﻿using BaseUtils.Helper;
using DatabaseCore.Modal;
using DatabaseCore.Sqlite;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web.Http;

namespace Service.src.Flow.Api
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class DataManageApi
    {

        /// <summary>
        /// 查找数据库中的所有表表名
        /// </summary>
        /// <returns></returns>
        public string FindTableNames()
        {
            List<SystemTable> systemTables = new DataManageService().FindTableNames(SqliteHelper.SystemDefaultDBID);
            if (systemTables.Count > 0)
            {
                systemTables = systemTables.Where(s =>
                {
                    bool pa = !AppData.IgnoreSystemTables.Contains(s.name);
                    if (pa)
                    {
                        foreach (string item in AppData.IgnoreSystemTablePrefix)
                        {
                            if (!pa)
                            {
                                break;
                            }
                            pa = !s.name.StartsWith(item);
                        }
                    }
                    return pa;
                }).ToList();
            }
            return systemTables.ToJson();
        }

        ///// <summary>
        ///// 获取表结构
        ///// </summary>
        ///// <param name="TableName"></param>
        ///// <returns></returns>
        //public string FindTableStructure(string TableName)
        //{
        //    List<TableStructure> TableStructures = new DataManageService().FindTableStructures(SqliteHelper.SystemDefaultDBID, TableName);
        //    return TableStructures.ToJson();
        //}

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="TableName"></param>
        /// <param name="Page"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        //public string QueryBySql(string Sql)
        //{
        //    return new DataManageService().QueryBySql(SqliteHelper.SystemDefaultDBID, Sql).ToJson();
        //}

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="Sql"></param>
        /// <param name="TableName"></param>
        /// <param name="Page"></param>
        /// <param name="Size"></param>
        /// <returns></returns>
        //public string PageQueryBySql(string Sql, string TableName, int Page, int Size)
        //{
        //    return new DataManageService().PageQueryBySql(SqliteHelper.SystemDefaultDBID, Sql, TableName, Page, Size).ToJson();
        //}

        /// <summary>
        /// 执行SQL
        /// 用在新增，删除，修改等
        /// </summary>
        /// <param name="Sql"></param>
        /// <returns></returns>
        public int ExecuteSql(string Sql)
        {
            return new DataManageService().ExecuteSql(SqliteHelper.SystemDefaultDBID, Sql);
        }

    }
}
